package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import model.TOption;
import model.TTitle;
import model.VTitle;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.GenerateUUID;
import util.toolkit.ResponseJSON;

/**
 * 题目信息处理类
 * @author 徐震霖
 * @since 2019-02-21
 */
@Controller
@RequestMapping(value="/titlemanagement")
public class SysTitleManagementController {

	/**
	 * 获取题目信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 获取题目信息
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "获取题目信息", 
			operationClassPath = "controller.SysTitleManagementController.getAllTitleList(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value = "/getalltitlelist")
	public String getAllTitleList(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		int currentPage = 1;
		int pageSize = 10;
		
		try {
			String titleName = req.getParameter("titleName");
			String titleType = req.getParameter("titleType");
			String dimensionid = req.getParameter("dimensionid");
			String limit = req.getParameter("limit");
			
			String page = req.getParameter("page");
			if(page != null && !page.equals("")){
				currentPage = Integer.parseInt(page);
			}
			if(limit != null && !limit.equals("")){
				pageSize = Integer.parseInt(limit);
			}
			if (titleName != null && !titleName.equals("") && !titleName.equals("0")) {
				exp.andLike("titleName", titleName, String.class);
			}
			if (titleType != null && !titleType.equals("") && !titleType.equals("0")) {
				exp.andEqu("titleType", titleType, Integer.class);
			}
			if (dimensionid != null && !dimensionid.equals("") && !dimensionid.equals("0")) {
				exp.andEqu("dimensionid", dimensionid, Integer.class);
			}
			String scaleTitleTypeid = req.getParameter("titletypeid");
			if(scaleTitleTypeid != null && !scaleTitleTypeid.equals("")){
				exp.andEqu("scaleTitleTypeid", scaleTitleTypeid, Integer.class);
			}
			List<VTitle> titlelist = 
					DAOFactory.getITitleDAO().getTitlesByPage(exp.toString(), currentPage, pageSize);
			List<VTitle> titlesize = 
					DAOFactory.getITitleDAO().getTitles(exp.toString());
			if (titlelist != null && titlelist.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", titlesize.size());
				JSONArray jsonArray = JSONArray.fromObject(titlelist);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到题目信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，题目信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 根据题目编号获取选项信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 根据题目编号获取选项信息
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "根据题目编号获取选项信息", 
			operationClassPath = "controller.SysTitleManagementController.getOptionByTitle(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value="/getoptionbytitle")
	public String getOptionByTitle(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		try {
			String titleid = req.getParameter("titleid");
			if (titleid != null && !titleid.equals("") && !titleid.equals("0")) {
				exp.andEqu("titleid", titleid, String.class);
			}
			exp.orderByAsc("optionNumber");
			List<TOption> options = DAOFactory.getITitleDAO().getOptions(exp.toString());
			if (options != null && options.size() > 0) {
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", options.size());
				JSONArray jsonArray = JSONArray.fromObject(options);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到选项信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，选项信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 添加一个题目信息(旧的，题目选项一起添加的)
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
//	@RequestMapping(value="/addtitle")
//	public String addTitle(Model model, HttpServletRequest req,
//			HttpServletResponse resp) throws IOException {
//		req.setCharacterEncoding("utf-8");
//		resp.setCharacterEncoding("utf-8");
//		resp.setContentType("application/json");
//		
//		JSONObject result = new JSONObject();
//		
//		try {
//			String titleName = req.getParameter("titlename");
//			String titleDescribe = req.getParameter("titledescribe");
//			String titleNumber = req.getParameter("titlenumber");
//			String titleType = req.getParameter("titletype");
//			String optionNumber = req.getParameter("optionnumber");
//			String optionContent = req.getParameter("optioncontent");
//			String dimensionid = req.getParameter("dimensionid");
//			
//			
//			if (titleName != null && !titleName.equals("") && titleDescribe != null && !titleDescribe.equals("") 
//					&& titleNumber != null && !titleNumber.equals("") && !titleNumber.equals("0") 
//					&& titleType != null && !titleType.equals("") && !titleType.equals("0") && 
//					optionNumber != null && !optionNumber.equals("") && !optionNumber.equals("0") 
//					&& optionContent != null && !optionContent.equals("") && dimensionid != null && 
//					!dimensionid.equals("") && !dimensionid.equals("0")) {
//				TTitle title = new TTitle();
//				title.setTitleName(titleName);
//				title.setTitleDescribe(titleDescribe);
//				title.setTitleNumber(Integer.parseInt(titleNumber));
//				title.setTitleType(Integer.parseInt(titleType));
//				title.setOptionNumber(Integer.parseInt(optionNumber));
//				title.setOptionContent(optionContent);
//				title.setDimensionid(Integer.parseInt(dimensionid));
//				
//				List<TOption> options = new ArrayList<TOption>();
//				String[] optionContents = optionContent.split(Constant.STRING_SPLIT);
//				for (String string : optionContents) {
//					String[] optionCons = string.split(Constant.STRING_SPLIT_OPTION);
//					TOption option = new TOption();	
//					for(int i = 0; i < optionCons.length; i++){											
//						switch (i) {
//						case 0:
//							option.setOptionNumber(Integer.parseInt(optionCons[i]));							
//							break;
//						case 1:
//							option.setOptionContent(optionCons[i]);
//							break;
//						case 2:
//							option.setOptionScore(Integer.parseInt(optionCons[i]));
//							break;
//						}
//					}
//					options.add(option);
//				}
//				
//				if(DAOFactory.getITitleDAO().addTitle(title, options)){
//					result.put("code", ResponseJSON.FLAG_SUCC);
//					result.put("msg", "题目信息添加成功！");
//				}
//				else {
//					result.put("code", ResponseJSON.FLAG_FAIL);
//					result.put("msg", "出现错误，题目信息添加失败，请重试！");
//				}
//			}
//			else {
//				result.put("code", ResponseJSON.FLAG_FAIL);
//				result.put("msg", "必填项不能为空，题目信息添加失败！");
//			}
//		} catch (Exception e) {
//			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
//			result.put("msg", "出现异常，题目信息添加失败，请重试！");
//			e.printStackTrace();
//		}
//		
//		PrintWriter out = resp.getWriter();
//		out.println(result);
//		return null;
//	}
	
	/**
	 * 添加一个题目信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 添加一个题目信息
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "添加一个题目信息！", 
			operationClassPath = "controller.SysTitleManagementController.addTitle(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息添加")
	@RequestMapping(value="/addtitle")
	public String addTitle(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String titleName = req.getParameter("titlename");
			String titleDescribe = req.getParameter("titledescribe");
			String titleNumber = req.getParameter("titlenumber");
			String titleType = req.getParameter("titletype");
			
			String scaleTitleTypeid = req.getParameter("paperType");
			String dimensionid = req.getParameter("dimensionid");//维度编号
			
			if (titleName != null && !titleName.equals("") && titleDescribe != null && !titleDescribe.equals("") 
					&& titleNumber != null && !titleNumber.equals("") && !titleNumber.equals("0") 
					&& titleType != null && !titleType.equals("") && !titleType.equals("0") && scaleTitleTypeid != null && 
					!scaleTitleTypeid.equals("") && !scaleTitleTypeid.equals("0") && dimensionid != null && !dimensionid.equals("") 
					&& !dimensionid.equals("0")) {
				TTitle title = new TTitle();
				GenerateUUID uuid = new GenerateUUID();
				//是否是反选题
				//TODO 徐震霖 2019-03-18 14:54修改开始
				title.setIsReverse(0);
				//TODO 徐震霖 2019-03-18 14:54修改结束
				title.setTitleid(uuid.getUUID());
				title.setTitleName(titleName);
				title.setTitleDescribe(titleDescribe);
				title.setTitleNumber(Integer.parseInt(titleNumber));
				title.setTitleType(Integer.parseInt(titleType));
				
				title.setScaleTitleTypeid(Integer.parseInt(scaleTitleTypeid));
				title.setOptionNumber(0);

				if(DAOFactory.getITitleDAO().addTitle(title, Integer.parseInt(dimensionid))){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "题目信息添加成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，题目信息添加失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空，题目信息添加失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "出现异常，题目信息添加失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 题目信息修改处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 题目信息修改处理类
	@Log(operationType = Constant.OPERTATION_UPDATE, operationDescribe = "修改一个题目信息！", 
			operationClassPath = "controller.SysTitleManagementController.updateTitle(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value="/updatetitle")
	public String updateTitle(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String titleid = req.getParameter("titleid");
			String titleName = req.getParameter("titlename");
			String titleDescribe = req.getParameter("titledescribe");
			String titleNumber = req.getParameter("titlenumber");
			String scaleTitleTypeid = req.getParameter("dimensionid");
			if (titleid != null && !titleid.equals("") && !titleid.equals("0") && titleName != null 
					&& !titleName.equals("") && titleDescribe != null && !titleDescribe.equals("") 
					&& titleNumber != null && !titleNumber.equals("") && !titleNumber.equals("0") 
					&& scaleTitleTypeid != null && !scaleTitleTypeid.equals("") && !scaleTitleTypeid.equals("0")) {
				TTitle title = DAOFactory.getITitleDAO().getTTitle(titleid);
				title.setTitleName(titleName);
				title.setTitleDescribe(titleDescribe);
				title.setTitleNumber(Integer.parseInt(titleNumber));
				
				title.setScaleTitleTypeid(Integer.parseInt(scaleTitleTypeid));
				if(DAOFactory.getITitleDAO().updateTitle(title)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "题目信息修改成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，题目信息修改失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空，题目信息修改失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_UNKNOWN_ERORR);
			result.put("msg", "出现异常，题目信息修改失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 添加一个题目选项信息
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 添加一个题目选项信息
	@Log(operationType = Constant.OPERTATION_ADD, operationDescribe = "添加一个题目选项信息！", 
			operationClassPath = "controller.SysTitleManagementController.addOption(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value="/addoption")
	public String addOption(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String optionNumber = req.getParameter("optionnumber");
			String optionContent = req.getParameter("optioncontent");
			String optionScore = req.getParameter("optionscore");
			String titleid = req.getParameter("titleid");
			
			if (optionNumber != null && !optionNumber.equals("") && !optionNumber.equals("0") && optionContent != null && 
					optionScore != null && !optionScore.equals("") && titleid != null && !titleid.equals("") 
					&& !titleid.equals("0")) {
				TOption option = new TOption();
				option.setOptionNumber(Integer.parseInt(optionNumber));
				option.setOptionContent(optionContent);
				option.setOptionScore(Integer.parseInt(optionScore));
				option.setTitleid(titleid);
				if(DAOFactory.getITitleDAO().addOption(option)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "选项信息添加成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，选项信息添加失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空，选项信息添加失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，选项信息添加失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 选项信息修改处理方法
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 选项信息修改处理方法
	@Log(operationType = Constant.OPERTATION_UPDATE, operationDescribe = "修改一个题目选项信息！", 
			operationClassPath = "controller.SysTitleManagementController.updateOption(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value="/updateoption")
	public String updateOption(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String optionid = req.getParameter("optionid");
			String optionNumber = req.getParameter("optionnumber");
			String optionContent = req.getParameter("optioncontent");
			String optionScore = req.getParameter("optionscore");
			String titleid = req.getParameter("titleid");
			
			if (optionid != null && !optionid.equals("") && !optionid.equals("0") && optionNumber != null 
					&& !optionNumber.equals("") && !optionNumber.equals("0") && optionContent != null && 
					optionScore != null && !optionScore.equals("") && titleid != null && !titleid.equals("") 
					&& !titleid.equals("0")) {
				TOption option = DAOFactory.getITitleDAO().getTOption(optionid);
				option.setOptionNumber(Integer.parseInt(optionNumber));
				option.setOptionContent(optionContent);
				option.setOptionScore(Integer.parseInt(optionScore));
				option.setTitleid(titleid);
				if(DAOFactory.getITitleDAO().updateOption(option)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "选项信息修改成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，选项信息修改失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "必填项不能为空，选项信息修改失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，选项信息修改失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 删除选项信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 删除选项信息处理类
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除一个题目选项信息！", 
			operationClassPath = "controller.SysTitleManagementController.deleteOption(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value="/deleteoption")
	public String deleteOption(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String optionid = req.getParameter("optionid");
			if (optionid != null && !optionid.equals("")) {
				if(DAOFactory.getITitleDAO().deleteOption(optionid)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "选项信息删除成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，选项信息删除失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "选项编号不能为空，选项信息删除失败，请重试！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，选项信息删除失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 题目信息删除处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 题目信息删除处理类
	@Log(operationType = Constant.OPERTATION_DELETE, operationDescribe = "删除一个题目信息！", 
			operationClassPath = "controller.SysTitleManagementController.deleteTitle(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value="/deletetitle")
	public String deleteTitle(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String titleid = req.getParameter("titleid");
			if (titleid != null && !titleid.equals("")) {
				if(DAOFactory.getITitleDAO().deleteTitle(titleid)){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "题目信息信息删除成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "出现错误，题目信息信息删除失败，请重试！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "题目编号不能为空，题目信息信息删除失败，请重试！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，题目信息信息删除失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}
	
	/**
	 * 设置一个题目反选或取消反选
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@Log(operationType = Constant.OPERTATION_UPDATE, operationDescribe = "设置一个题目反选或取消反选！", 
			operationClassPath = "controller.SysTitleManagementController.enableOrDisEnableReverse(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "题库管理/题目信息查看")
	@RequestMapping(value="/enableordisenablereverse")
	public String enableOrDisEnableReverse(Model model, HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		
		try {
			String titleid = req.getParameter("titleid");
			String isReverse = req.getParameter("isreverse");
			if (titleid != null && !titleid.equals("") 
					&& !titleid.equals("0") && isReverse.equals("0") || isReverse.equals("1")) {
				if(DAOFactory.getITitleDAO().enableOrDisEnableReverse(titleid, Integer.parseInt(isReverse))){
					result.put("code", ResponseJSON.FLAG_SUCC);
					result.put("msg", "题目反选成功！");
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "题目反选失败！");
				}
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "题目反选失败！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "题目反选失败！");
			e.printStackTrace();
		}
		PrintWriter writer=resp.getWriter();
		writer.print(result);
		return null;		
	}
	
}
